
#include "CsvLoader.h"
#include "CsvReader.h"

/*
 * Class:     CsvReader
 * Method:    newCsvLoader
 * Signature: (Ljava/lang/String;)Ljava/lang/Object;
 */
JNIEXPORT jlong JNICALL Java_CsvReader_newCsvLoader(JNIEnv *env, jobject object, jstring path)
{
    const char *spath = env->GetStringUTFChars(path, NULL);
    void *obj = (void *)new CsvLoader((char *)spath);
    return (jlong)obj;
}

/*
 * Class:     CsvReader
 * Method:    freeCsvLoader
 * Signature: (Ljava/lang/Object;)V
 */
JNIEXPORT void JNICALL Java_CsvReader_freeCsvLoader(JNIEnv *env, jobject object, jlong self)
{
    CsvLoader *obj = (CsvLoader *)self;
    if (obj != NULL) delete obj;
}

/*
 * Class:     CsvReader
 * Method:    cReadRow
 * Signature: (Ljava/lang/Object;)[Ljava/lang/String;
 */
JNIEXPORT jobjectArray JNICALL Java_CsvReader_cReadRow(JNIEnv *env, jobject object, jlong self)
{
    CsvLoader *obj = (CsvLoader *)self;
    std::vector<char *> *row = obj->read_row();
    if (row == NULL) return NULL;
    jclass jlstr = env->FindClass("java/lang/String");
    jobjectArray ret = env->NewObjectArray(row->size(), jlstr, NULL);
    for (int i = 0; i < row->size(); i++) {
        char *fd = (*row)[i];
        jstring value = env->NewStringUTF(fd);
        env->SetObjectArrayElement(ret, i, value);
    }
    return ret;
}

/*
 * Class:     CsvReader
 * Method:    cSize
 * Signature: (Ljava/lang/Object;)J
 */
JNIEXPORT jlong JNICALL Java_CsvReader_cSize(JNIEnv *env, jobject object, jlong self)
{
    CsvLoader *obj = (CsvLoader *)self;
    return (jlong)(obj->size());
}

